// pages/docDetail/docDetail.js
import Toast from 'tdesign-miniprogram/toast/index';
import { get } from '../../utils/request';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    docInfo: {},
    visible: false,
    note: '',
    time: '',
    confirmBtn: { content: '确定预约', variant: 'base' },
    dialogKey: '',
    showConfirm: false,
    docDetail: {},
    restStamp: 0,
    selectDate: '',
    avator: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      docInfo: JSON.parse(options.item),
    });

    wx.request({
      url: `http://8.130.99.108:8090/api/getDoctorDetail`, //仅为示例，并非真实的接口地址
      data: {
        doctorId: JSON.parse(options.item).doctorId,
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: 'GET',
      success: (res) => {
        console.log(res.data);
        if (res.data.message == '服务运行成功') {
          this.setData({
            docDetail: res.data.data,
            avator: res.data.data.avatar,
          });
        }
      },
      fail: (error) => {
        console.log(error);
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},

  handleCalendar(e) {
    console.log(e);
    //  区分上午下午晚上
    const { time } = e.currentTarget.dataset;
    this.setData({
      time,
    });
    this.setData({ visible: true });
  },
  handleConfirm(e) {
    const { value } = e.detail;
    const format = (val) => {
      const date = new Date(val);
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
    };
    const date = format(value); //2023-3-20
    this.setData({
      selectDate: date,
    });
    const { docInfo, time } = this.data;

    wx.request({
      url: `http://8.130.99.108:8090/api/getSlots`, //仅为示例，并非真实的接口地址
      data: {
        doctorId: docInfo.doctorId,
        amOrPm: time,
        date,
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: 'GET',
      success: (res) => {
        console.log(res.data);
        if (res.data.message == '服务运行成功') {
          this.setData({
            restStamp: res.data.data,
          });
        }
      },
      fail: (error) => {
        console.log(error);
      },
    });
  },
  onClose({ detail }) {
    console.log(detail.trigger);
  },
  showDialog(e) {
    const { key, item } = e.currentTarget.dataset;
    console.log(item);
    if (!this.data.selectDate) {
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请选择时间后再进行预约',
      });
      return;
    }
    this.setData({ [key]: true, dialogKey: key });
  },
  closeDialog() {
    const { dialogKey } = this.data;
    this.setData({ [dialogKey]: false });
  },
  confirmPre() {
    const { dialogKey } = this.data;
    this.setData({ [dialogKey]: false });
    const { docInfo, selectDate, time } = this.data;
    const personId = wx.getStorageSync('personId');

    wx.request({
      url: `http://8.130.99.108:8090/api/appoint`, //仅为示例，并非真实的接口地址
      data: {
        doctorId: docInfo.doctorId + '',
        amOrPm: time,
        date: selectDate,
        patientId: personId + '',
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: 'POST',
      success: (res) => {
        console.log(res.data);
        if (res.data.message == '服务运行成功') {
          Toast({
            context: this,
            selector: '#t-toast',
            message: '预约成功,请前往预约记录查看',
          });
        } else {
          Toast({
            context: this,
            selector: '#t-toast',
            message: '预约失败,请稍后再试',
          });
        }
      },
      fail: (error) => {
        console.log(error);
      },
    });

    // post('/appoint', {
    //   doctorId: docInfo.doctorId,
    //   amOrPm: time,
    //   date: selectDate,
    //   paitentId: personId,
    // }).then((res) => {
    //   if (res.message == 'success') {
    //     this.setData({
    //       restStamp: res.data,
    //     });
    //     Toast({
    //       context: this,
    //       selector: '#t-toast',
    //       message: '预约成功,请前往预约记录查看',
    //     });
    //   } else {
    //     Toast({
    //       context: this,
    //       selector: '#t-toast',
    //       message: '预约失败,请稍后再试',
    //     });
    //   }
    // });
  },
});
